package com.business.system.action;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.servlet.http.HttpServletRequest;

import net.sf.rose.jdbc.DBUtils;
import net.sf.rose.jdbc.KeyGenerator;
import net.sf.rose.jdbc.PageBean;
import net.sf.rose.jdbc.service.Service;
import net.sf.rose.util.StringUtil;
import net.sf.rose.web.utils.WebUtils;

import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.business.system.bean.BrandBean;
import com.business.system.util.DBTools;
import com.business.system.util.FileUtil;
import com.business.system.util.PageUtil;

/**
 * @author: xudy
 * @date: 2018/04/11 13:37
 * @description:
 */
@RestController
@RequestMapping("brand")
public class BrandAction extends BaseAction {

    /**
     * 获取全部品牌
     * @param service
     * @param request
     * @return
     */
    @RequestMapping("list.do")
    public List<BrandBean> list(Service service, HttpServletRequest request){
        List<BrandBean> list = DBUtils.getBeanList(service,BrandBean.class);
        return list;
    }


    /**
     * @Description:获取品牌，分页
     * @Author: zhangqiang
     * @Date: 16:16 2018/4/14
     */
    @RequestMapping("getBrandList.do")
    public PageBean getBrandList(Service service, HttpServletRequest request) {
        PageBean page = PageUtil.page(request);
        StringBuilder sql = new StringBuilder();
        sql.append("select * from brand where 1=1");
        Map<String, Object> params = WebUtils.getRequestData(request);
        List<Object> condition = new ArrayList<>();
        DBTools.getDataList(service, sql.toString(), page);
        return page;
    }


    /**
     * @Description:保存品牌
     * @Author: zhangqiang
     * @Date: 13:10 2018/4/16
     */
    @RequestMapping("saveBrand.do")
    public int saveBrand(Service service, HttpServletRequest request,BrandBean brandBean ) {
       if(StringUtil.isEmpty(brandBean.getId())){
           brandBean.setId(KeyGenerator.getSystemUUID());
           return  DBTools.insertBean(service, brandBean);
       }
        else{
           return  DBTools.updateBean(service, brandBean);
       }

    }


    @RequestMapping(value ="savePicBrand.do",method = RequestMethod.POST)
    public int savePicBrand(Service service, HttpServletRequest request) {
        //额外的数据
        Map<String,Object> data = WebUtils.getRequestData(request);
        BrandBean brandBean = new BrandBean();
        brandBean.setBrandName(data.get("BRAND_NAME").toString());
        brandBean.setAgentFee(Double.parseDouble(data.get("AGENT_FEE").toString()));
        brandBean.setAgentRate(Double.parseDouble(data.get("AGENT_RATE").toString()));
        brandBean.setFranchiseFee(Double.parseDouble(data.get("FRANCHISE_FEE").toString()));
        brandBean.setBrandDesc(data.get("BRAND_DESC")==null?"":data.get("BRAND_DESC").toString());

        brandBean.setBrandLogo(data.get("BRAND_LOGO")==null?"":data.get("BRAND_LOGO").toString());

        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();

        //有图片则上传图片
        if(fileMap!=null){
            String ctxPath = FileUtil.getUploadFilePath();

            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
            String ymd = sdf.format(new Date());
            ctxPath += File.separator + ymd + File.separator;
            // 创建文件夹
            File file = new File(ctxPath);
            if (!file.exists()) {
                file.mkdirs();
            }
            String fileName = null;
            UUID uuid = null;
            for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
                // 上传文件
                MultipartFile mf = entity.getValue();
                fileName = mf.getOriginalFilename();// 获取原文件名
                fileName = fileName.substring(fileName.lastIndexOf("."));
                uuid = UUID.randomUUID();
                File uploadFile = new File(ctxPath + uuid + fileName);
                try {
                    FileCopyUtils.copy(mf.getBytes(), uploadFile);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            String path = ymd + "/" + uuid + fileName;

            brandBean.setBrandLogo(path);

            //新增品牌
            if(data.get("ID")==null){
                brandBean.setId(KeyGenerator.getSystemUUID());
                return  DBTools.insertBean(service, brandBean);
            }
            else{
                brandBean.setId(data.get("ID").toString());
                return  DBTools.updateBean(service, brandBean);
            }

        }
        else{
            //更新
            brandBean.setId(data.get("ID").toString());
            return  DBTools.updateBean(service, brandBean);
        }

    }


    /**
     * @Description:删除品牌
     * @Author: zhangqiang
     * @Date: 14:39 2018/4/16
     */
    @RequestMapping("deleteBrand.do")
    public int deleteBrand(Service service, HttpServletRequest request,BrandBean brandBean ) {
            return  DBTools.deleteBean(service, brandBean);
    }


}